<template>
  <TreeListLayout
    padding="0"
    :showTree="false">
    <div slot="layout-content">
      <ModalResources
        :formItem="formItem"
        ref="modalResource" />
      <div class="iv-step-footer">
        <Button
          type="primary"
          @click="onOk"
          class="iv-pull-right iv-m-t-12"
          size="large">保存</Button>
        <Button
          type="ghost"
          @click="onCancel"
          class="iv-pull-right iv-m-t-12 iv-m-r-12"
          size="large">取消</Button>
      </div>
    </div>
  </TreeListLayout>
</template>
<script>
/*
 * @description 引入iconfont组件
 */
import TreeListLayout from '@/page/business/treelist-layout'
/*
 * @description 引入表单资源组件
 */
import ModalResources from './modalResources.vue'
/*
 * @description 模板接口前缀
 */
const requestPrefix = 'http://www.doclever.cn:8090/mock/5b484584362f91495c091d11/getList'

export default {
  name: 'routerAddDetail',
  data () {
    return {
      formItem: {
        user: {
          userInfo: ''
        },
        regionList: [{
          label: '中国',
          value: 'Child'
        }, {
          label: '日本',
          value: 'Japen'
        }, {
          label: '美国',
          value: 'USA'
        }]
      }
    }
  },
  created () {
    this.isEdit = this.$route.query.id
    this.isEdit && this.getDetail()
  },
  components: {
    TreeListLayout,
    ModalResources
  },
  methods: {
    getDetail () {
      this.$ajax({
        url: `${requestPrefix}`,
        method: 'GET',
        data: this.$route.query
      }).then(({data}) => {
        this.formItem = data
      })
    },
    onCancel () {
      this.$router.back()
    },
    onOk () {
      //校验规则
      this.$refs.modalResource.validateFrom().then(validate => {
        if (!validate) return
        //发送请求保存
        this.$ajax({
          url: `${requestPrefix}`,
          method: this.isEdit ? 'PUT' : 'POST',
          data: {}
        }).then(({data}) => {
          this.$router.back()
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
